/**
 * 桨模 编辑页
 *
 * 作者 ganjun
 * 日期 2024-11-18 22:28:43
 */
layui.use(['jquery','febs', 'form', 'validate', 'fileUpload'], function () {
    var $ = layui.$,
        febs = layui.febs,
        layer = layui.layer,
        form = layui.form,
        $view = $('#modelOars-edit'),
        $makeModelFile = $view.find('#makeModelFileSelect'),
        $source = $view.find('#sourceSelect'),
        $position = $view.find('#positionSelect'),
        $opinion = $view.find('#opinionSelect'),
        fileUpload = layui.fileUpload,
        laydate = layui.laydate,
        validate = layui.validate;

    form.verify(validate);
    form.render();

    initDictItemSelect();

    initFormValue();

    function initFormValue() {
        if (modelOarsData) {
            form.val("modelOars-edit-form", {
                "id": modelOarsData.id,
                "oarsCode": modelOarsData.oarsCode,
                "projectCode": modelOarsData.projectCode,
                "oarsName": modelOarsData.oarsName,
                "source": modelOarsData.source,
                "status": modelOarsData.status,
                "demandEnddate": modelOarsData.demandEnddate,
                "actuallyStartdate": modelOarsData.actuallyStartdate,
                "actuallyEnddate": modelOarsData.actuallyEnddate,
                "makeModelFile": modelOarsData.makeModelFile,
                "waterDate": modelOarsData.waterDate,
                "position": modelOarsData.position,
                "diameter": modelOarsData.diameter,
                "pitch": modelOarsData.pitch,
                "surfaceRate": modelOarsData.surfaceRate,
                "chordLength": modelOarsData.chordLength,
                "fanThick": modelOarsData.fanThick,
                "holeDiameter": modelOarsData.holeDiameter,
                "shellLength": modelOarsData.shellLength,
                "frontDiameter": modelOarsData.frontDiameter,
                "backendDiameter": modelOarsData.backendDiameter,
                "oarsType": modelOarsData.oarsType,
                "prupose": modelOarsData.prupose,
                "oarsDiameter": modelOarsData.oarsDiameter,
                "fanNumber": modelOarsData.fanNumber,
                "memo": modelOarsData.memo,
                "origOarsCode": modelOarsData.origOarsCode,

            });
            if(modelOarsData.source){
                changeSource(modelOarsData.source.toString());
            }

        }

        if(jmNo){
            form.val("modelOars-edit-form", {
                "oarsCode": jmNo,
            });
        }
    }

    function initDictItemSelect() {
        if(modelOarsData){
            //来源
            febs.initDictItemSelect($source, 'MODEL_OARS_SOURCE', modelOarsData.source, null);
            //桨模位置
            febs.initDictItemSelect($position, 'MODEL_OARS_POSITION', modelOarsData.position, null);
            //建模文件
            febs.initDictItemSelect($makeModelFile, 'MODEL_OARS_FILE', modelOarsData.makeModelFile, null);
            //处理意见
            febs.initDictItemSelect($opinion, 'MODEL_OARS_OPINION', modelOarsData.opinion, null);
        }else{
            //来源
            febs.initDictItemSelect($source, 'MODEL_OARS_SOURCE', null, null);
            //桨模位置
            febs.initDictItemSelect($position, 'MODEL_OARS_POSITION', null, null);
            //建模文件
            febs.initDictItemSelect($makeModelFile, 'MODEL_OARS_FILE', null, null);
            //处理意见
            febs.initDictItemSelect($opinion, 'MODEL_OARS_OPINION', null, null);
        }

    }

    form.on('select(sourceSelect)', function (data) {
        var val = data.value;
        changeSource(val);
    });

    function changeSource(val){
        if(val === '2'){
            $('#origOarsCodeDiv').show();
        }else{
            $('#origOarsCodeDiv').hide();
        }
    }

    $('#origOarsCode').on('input',function(){
        var val = $(this).val();
        febs.get(ctx + 'modelOars/getModelOarsByCode/' + val ,null, function (res) {
            if(res.code === 200){
                var resData = res.data;
                if (resData) {
                    form.val("modelOars-edit-form", {
                        "demandEnddate": resData.demandEnddate,
                        "actuallyStartdate": resData.actuallyStartdate,
                        "actuallyEnddate": resData.actuallyEnddate,
                        "makeModelFile": resData.makeModelFile,
                        "waterDate": resData.waterDate,
                        "position": resData.position,
                        "diameter": resData.diameter,
                        "pitch": resData.pitch,
                        "surfaceRate": resData.surfaceRate,
                        "chordLength": resData.chordLength,
                        "fanThick": resData.fanThick,
                        "holeDiameter": resData.holeDiameter,
                        "shellLength": resData.shellLength,
                        "frontDiameter": resData.frontDiameter,
                        "backendDiameter": resData.backendDiameter,
                        "oarsType": resData.oarsType,
                        "prupose": resData.prupose,
                        "oarsDiameter": resData.oarsDiameter,
                        "fanNumber": resData.fanNumber,
                        "memo": resData.memo,
                        "opinion": resData.opinion,
                    });
                }

            }
        }, null);

    });

    form.on('submit(modelOars-edit-form-submit)', function (data) {
        addOrUpdate(data.field);
        return false;
    });

    var fileUploadDiv = fileUpload.render({
        elem: '#fileUpload',
        url: ctx + 'upload/file',
        type: 2,//0-图片，1-视频，2-其他
        maxNum:5,
        defaultData: getFileList(),
        done: function (res) {
        },
    });

    function addOrUpdate(data) {
        var params = {};
        params.modelOars = data;
        params.fileList = getFileData();
        if(data && data.source === '2'){
            if(data.origOarsCode == null || data.origOarsCode == ''){
                febs.alert.warn('已有桨模，原桨模编号不能为空');
                return;
            }
        }
        febs.modal.confirm("保存提示！","是否保存？",function (index){
            data.projectName = $view.find("select[name='projectCode']").find("option:selected").text();
            if (data.id && data.id.length > 0) {
                febs.postArray(ctx + 'modelOars/update', params, function () {
                    layer.closeAll();
                    febs.alert.success('修改成功');
                    // febs.reloadPage();
                    var currentUrl = window.location.hash.replace('#', '');
                    febs.view.tab.del(currentUrl);
                    // febs.view.tab.change("/modelOars");
                    $('#febs-modelOars').find('#query').trigger('click');
                });
            } else {
                febs.postArray(ctx + 'modelOars', params, function () {
                    layer.closeAll();
                    febs.alert.success('新增成功');
                    var currentUrl = window.location.hash.replace('#', '');
                    febs.view.tab.del(currentUrl);
                    // febs.view.tab.change("/modelOars");
                    $('#febs-modelOars').find('#query').trigger('click');
                });
            }
        });
    }

    function getFileList() {
        if (attachs && attachs.length>0) {
            return  attachs;
        }else {
            return  [];
        }
    }

    /**
     * 获取附件
     * @returns {*[]}
     */
    function getFileData(){
        var fileDatas = $view.find(".viewFile");
        var fileList = [];
        $.each(fileDatas,function (index, item){
            var data = {
                filePath: item.dataset.filepath,
                fileName: item.dataset.filename,
            }
            fileList.push(data);
        })
        return fileList;
    }

    laydate.render({
        elem: "#waterDate",
        type: 'datetime',
        format: 'yyyy-MM-dd HH:mm',
    });

    laydate.render({
        elem: "#demandEnddate",
        type: 'datetime',
        format: 'yyyy-MM-dd HH:mm',
    });

    $('#reset').click(function () {
        closeCur();
    });

    function closeCur(){
        var currentUrl = window.location.hash.replace('#', '');
        febs.view.tab.del(currentUrl);
        // febs.view.tab.change("/modelOars");
        $('#febs-modelOars').find('#query').trigger('click');
    }
});